'use strict'

const router = require('koa-router')()
const fs = require('fs')

function getFilePath(filePath) {
    let files = fs.readdirSync(filePath)
    let resFile = files.filter(item => {
        return item.endsWith('.js') && item != 'index.js'
    })
    return resFile
}

function createRouter(resFile) {
    resFile.forEach(item => {
        let tmpPath = __dirname + '/' + item
        let Obj = require(tmpPath)
        for (let i in Obj) {
            let tmpArr = i.split(' ');
            let rMethod = tmpArr[0];
            let rPath = tmpArr[1];
            let rFunction = Obj[i]
            router[rMethod](rPath, rFunction)
        }
    });
}

let getPath = getFilePath(__dirname)
createRouter(getPath)

module.exports = router